//js
const app = getApp()

Page({
  

  data: {
      
  },




  

  onShow: function () {
    console.log(app.globalData.userInfo)

    


    if (app.globalData.userInfo) {

      this.setData({
        userInfo: app.globalData.userInfo,
        isLogin: true
      })
    }
  },


    getUserInfo: function (e) {

      var that=this;
      wx.login({
        success: function (res) {
          var code = res.code;//登录凭证
          if (code) {
            //2、调用获取用户信息接口
            wx.getUserInfo({
              success: function (res) {
                console.log({ encryptedData: res.encryptedData, iv: res.iv, code: code })
                //3.请求自己的服务器，解密用户信息 获取unionId等加密信息
                wx.request({
                  url: app.globalData.serDomain +'/user/getUserInfo',//自己的服务接口地址
                  method: 'post',
                  header: {
                    'content-type': 'application/x-www-form-urlencoded'
                  },
                  data: { encryptedData: res.encryptedData, iv: res.iv, code: code },
                  success: function (data) {

                     console.log(data)
                    //设置unionid到globaledata
                    var unionid = data.data.unionid
                    app.globalData.unionid = unionid
                    app.globalData.wxUser = data.data.wxUser


                    if(data.data.code==0){
                      4  //查询到用户信息，写入setData及globalData及Storage
                        var userInfo=data.data.userInfo
                       // console.log(userInfo);
                        //设置当前页面Data
                        that.setData({
                              userInfo: userInfo,
                              isLogin: true
                            })
                        //设置当前app生命周期的globalData
                        app.globalData.userInfo = userInfo
                        
                        //设置本地缓存storage
                       
                        wx.setStorage({ key: "userInfo", data: userInfo })
                    }else{
                       //没有查询到用户信息跳转到绑定页面
                      wx.navigateTo({
                        url: '../bind/bind',
                      })      
                         
                    }
                  
                  
                  },
                  fail: function () {
                    console.log('系统错误,请重试！')
                  }
                })
              },
              fail: function () {
                console.log('获取用户信息失败')
              }
            })

          } else {
            console.log('获取用户登录态失败！' + r.errMsg)
          }
        },
        fail: function () {
          console.log('登陆失败')
        }
      })



  }

})